﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace DataAccess
{
    class IOFile
    {
        public static string SysIOFile = System.AppDomain.CurrentDomain.BaseDirectory + @"SysIO.dat";
        public static string serverName,ID,passWord,database;
        /// <summary>
        /// 写信息
        /// </summary>
        /// <param name="logInfo">写入文件的内容</param>
        public void writeLog(string logInfo)
        {
            StreamWriter sw = new StreamWriter(SysIOFile, false, Encoding.Default);
            sw.Write("");
            sw.Close();
            if (File.Exists(SysIOFile))
            {
                writeLogToFile(logInfo);
            }
            else
            {
                createAndWriteLogFile(logInfo);
            }
        }

        /// <summary>
        /// 往文件中追写信息
        /// </summary>
        /// <param name="logInfo">写入文件的内容</param>
        static private void writeLogToFile(string logInfo)
        {
            try
            {
                using (StreamWriter m_streamWriter = File.AppendText(SysIOFile))
                {
                    m_streamWriter.Flush();
                    m_streamWriter.WriteLine(logInfo);
                    m_streamWriter.Flush();
                    m_streamWriter.Dispose();
                    m_streamWriter.Close();
                }
            }
            catch (System.Exception)
            {
                throw new Exception("文件写入失败！");
            }
        }

        /// <summary>
        /// 创建文件并写日志信息
        /// </summary>
        /// <param name="logInfo">写入文件的内容</param>
        static private void createAndWriteLogFile(string logInfo)
        {
            FileStream fs = new FileStream(SysIOFile, FileMode.Create, FileAccess.Write);
            try
            {
                using (StreamWriter m_streamWriter = new StreamWriter(fs))
                {
                    m_streamWriter.Flush();
                    m_streamWriter.WriteLine(logInfo);
                    m_streamWriter.Flush();
                    m_streamWriter.Dispose();
                    m_streamWriter.Close();
                }
            }
            catch (Exception)
            {
                throw new Exception("文件写入失败！");
            }
            finally
            {
                fs.Dispose();
                fs.Close();
            }
        }
        public static void OutFile()
        {
            if (File.Exists(SysIOFile) == false)
            {
                return;
            }
            using (StreamReader sr = new StreamReader(new FileStream(SysIOFile, FileMode.OpenOrCreate, FileAccess.Read)))
            {
                string str = sr.ReadLine();
                if (str == null)
                {
                    return;
                }
                string[] inFile = str.Split(' ');
                if (inFile.Length == 0)
                {
                    serverName = "";
                    ID = "";
                    passWord = "";
                    database = "";
                }
                else
                {
                    serverName = inFile[0];
                    ID = inFile[1];
                    passWord = inFile[2];
                    database = inFile[3];
                }
            }
        }

    }
}
